/**
 * Created by Administrator on 2018/7/29/029.
 */


layui.use(['table', 'form', 'layer','element'], function () {

    // 操作对象
    var form = layui.form,
        table = layui.table,
        layer = layui.layer,
        element = layui.element,
        $ = layui.jquery;


    var token = $('#_token').val();
    var tableCate;
    var index;


    hljs.initHighlightingOnLoad();


    // 初始化
    getBlogList();


    form.on('radio(cate_id)',function (data) {
       if(data.value != 1){
           $('.key_box').hide();
           $('.note').show();
       }else{
           $('.note').hide();
           $('.key_box').show();
       }
    });

    //刷新
    $('.btn-refresh').on('click', function () {
        window.location.reload();
    });

    //
    element.on('tab(docDemoTabBrief)', function(data) {
        index = data.index;
        //分类信息
        if(data.index == 0){
            getBlogList();
        }
        if(data.index == 2){
            previewBlog()
        }

    });

    //提交
    form.on('submit(add)',function(data) {

        var param = data.field;
        param.key_id = getCheckboxParam();
        param.content = editor.$txt.html();
        param._token = token;
        addBlog(param);
    });

    //博客列表
    function getBlogList() {
        tableCate = table.render({
            elem: '#blog',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
            jump: function(obj){

            },
            cols: [[                  //标题栏
                {field: 'id', title: 'ID'}
                , {field: 'author', title: '作者'}
                , {field: 'title', title: '标题'}
                , {field: 'cate_name', title: '分类'}
                , {field: 'keyword', title: '标签'}
                , {field: 'note', title: '关键词'}
                , {field: 'created_at', title: '创建时间'}
                , {fixed: 'right', title: '操作', width: 220, align: 'center', toolbar: '#blog_tool'} //这里的toolbar值是模板元素的选择器

            ]],
            url: '/get_blog_list',
            where: {
                _token: token
            },    //查询参数
            method: 'get',
            done: function (res, curr, count) {

            }
        });
    }

    function getCheckboxParam(obj) {
        var param = [];
        $("input:checkbox[name='key_id']:checked").each(function() { // 遍历name=standard的多选框
            param.push($(this).val());
        });
        return param.toString();
    }

    function addBlog(data) {
        $.ajax({
            url:"/add_blog",
            data:data,
            type:'post',
            timeout:2000,
            success:function(data) {
                layer.msg(data.msg,{
                    time: 2000,
                    end:function(){
                        window.location.reload();
                    }
                });
            },
            error:function() {
                layer.msg('提交错误');
            }
        });

    }

    function previewBlog() {
        $.ajax({
            url:"/preview_blog",
            data:{
                _token:token,
                action:'last'
            },
            type:'post',
            timeout:2000,
            datatype:'json',
            success:function(data) {
                if(data.code == 0) {
                    var blog = data.data;
                    $('.pre_content').html(blog.content);
                    $('.pre_header .title b').html(blog.title);
                    $('.pre_header .time').html(blog.created_at)
                }
            },
            error:function() {

            }
        });
    }

    //文本编辑器
    wangEditor.config.printLog = false;
    var editor = new wangEditor('editor');
    editor.config.uploadImgUrl = '/upload';
    // editor.config.codeDefaultLang = 'javascript';
    editor.config.uploadParams = {
        _token: token
    };
    editor.create();


});